/**
 * @license
 * Copyright 2021 Du Tian Wei
 * SPDX-License-Identifier: Apache-2.0
 */

 (function () {
    Ublockly.AsyncParser = {};

    /**
     * 异步分析的setTimeout
     */
    Ublockly.AsyncParser.TimeoutHandler = 0;

    /**
     * 分析的延迟
     */
    Ublockly.AsyncParser.WaitingTime = 10000;
    Ublockly.AsyncParser.ParseNow = function (workspaceId) {
        clearTimeout(Ublockly.AsyncParser.TimeoutHandler);
        Ublockly.AsyncParser.TimeoutHandler = 0;
        var workspace = Blockly.Workspace.getById(workspaceId);
        if(workspace!=null){
            Ublockly.BlocklyParser.parseFromWorkspace(workspace);
        }
    };
    Ublockly.AsyncParser.SetTimeout = function (workspaceId) {
        clearTimeout(Ublockly.AsyncParser.TimeoutHandler);
        Ublockly.AsyncParser.TimeoutHandler =
            setTimeout(Ublockly.AsyncParser.ParseNow, Ublockly.AsyncParser.WaitingTime, workspaceId);
    };
    // Ublockly.Blocks.EventBaseListener = function (event) {
    //     if (event.type == Blockly.Events.BLOCK_MOVE ||
    //         event.type == Blockly.Events.BLOCK_CREATE ||
    //         event.type == Blockly.Events.BLOCK_CHANGE||
    //         event.type == Blockly.Events.VAR_CREATE||
    //         event.type == Blockly.Events.VAR_RENAME||
    //         event.type == Blockly.Events.MARKER_MOVE) {
    //             Ublockly.AsyncParser.SetTimeout(event.workspaceId);
    //     }
    // };
    // Ublockly.wsBuildCbs.push(function (workspace) {
    //     workspace.addChangeListener(Ublockly.Blocks.EventBaseListener);
    // });

})();